package leetcode.median;

import leetcode.base.TreeNode;

/**
 * ClassName: $114_Flatten
 * Description:
 * date: 2021-11-19 23:34
 *
 * @author liyifan
 */
public class $114_Flatten {

    TreeNode parent = new TreeNode(0);
    public void flatten(TreeNode root) {

        if (root == null)
            return;

        TreeNode tempLeft = root.left;
        TreeNode tempRight = root.right;

        parent.right = root;
        parent.left = null;
        parent = root;

        flatten(tempLeft);
        flatten(tempRight);
    }

    public void flatten2(TreeNode root) {
        if (root == null)
            return;

        flatten2(root.left);
        flatten2(root.right);

        TreeNode left = root.left;
        TreeNode right = root.right;

        root.left = null;
        root.right = left;

        TreeNode p = root;
        while (p.right != null) {
            p = p.right;
        }
        p.right = right;
    }
}
